﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using Dataglot;

namespace IrishSettings
{

    public class SettingsForApplicationController : TableController<GenericSettingStored>
    {
        public SettingsForApplicationController(DataFactory factory):base(factory, null,null)
        {
            
            ConstructTableManager();
        }

        public SettingsForApplicationController(DataFactory factory, DbConnection connection, DbTransaction transaction):base(factory, connection,transaction)
        {
            if(connection==null)
                throw new ArgumentException("connection parameter required, can't be null");
            sharedConnection = connection;
            ConstructTableManager();
        }

        private void ConstructTableManager()
        {
            tableName = "{$ns}_settings_values";

            columns = new Dictionary<string,DbType>
                          { 
                              {"id",DbType.Int32},
                              {"schema_id",DbType.Int32},
                              //{"name",DbType.String},
                              {"serialized_value",DbType.String},
                              {"version",DbType.String}
                              //{"scope",DbType.String}
                              //,
                            //  {"application",DbType.String},
                              //{"value_type",DbType.String},
                             // {"culture",DbType.String},
                          };
        }

        public override GenericSettingStored Select(long id)
        {
            return ProcessSelect(id);
        }

        public override List<GenericSettingStored> SelectList(int from, int to)
        {
            return ProcessSelectList(from, to);
        }

        public override GenericSettingStored Insert(GenericSettingStored setting)
        {
            return ProcessInsert(setting);
        }

        public override GenericSettingStored Update(GenericSettingStored setting)
        {
            return ProcessUpdate(setting);   
        }

        public override void Delete(long id)
        {
            ProcessDelete(id);
        }

        public override bool Exists(long id)
        {
            return ProcessExists(id);
        }

        public bool ExistsByName(string name, string application, Version version)
        {
            using(SelectApplicationSettings command = new SelectApplicationSettings(factory, sharedConnection,sharedTransaction))
            {
                return command.Exists(name, application, version);
            }
        }

        public GenericSettingStored SelectByName(string name, string application, Version version)
        {
            using (SelectApplicationSettings command = new SelectApplicationSettings(factory, sharedConnection, sharedTransaction))
            {
                long id;
                using(DbDataReader reader = command.SelectOne(name, application, version))
                {
                    reader.Read();
                    id = Convert.ToInt64(reader.GetValue(0));
                }
                return ProcessSelect(id);
            }
        }

        public void CopyValuesForNewVersion(SettingsApplication newApplication, Version oldVersion, Version newVersion)
        {
            using (SelectApplicationSettingValuesByVersion command = new SelectApplicationSettingValuesByVersion(factory, sharedConnection, sharedTransaction))
            {
                List<long> ids = command.Ids(newApplication.Application, oldVersion);
                foreach (long id in ids)
                {
                    GenericSettingStored item = Select(id);
                    item.Version = newVersion.ToString();
                    Insert(item);
                }
            }
        }
    }
}
